package com.es.servlet; /**
 * 描述:
 * es-jsp-
 *
 * @author Derek
 * 创建时间：2022-04-26 11:49
 */

import com.es.bean.Student;
import com.es.service.StudentService;
import com.es.service.impl.StudentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UpdateServlet", value = "/update")
public class UpdateServlet extends HttpServlet {

    StudentService service = new StudentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        int age = Integer.parseInt(request.getParameter("age"));
        String telephone = request.getParameter("telephone");
        String sex = request.getParameter("sex");
        //把收集的数据放入到 数据库中？
        Student student = new Student();
        student.setName(name);
        student.setAddress(address);
        student.setAge(age);
        student.setTelephone(telephone);
        student.setSex(sex);
        student.setId(id);
        //
        boolean flag = service.update(student);
        String msg = "更新失败";
        if(flag){
            msg = "更新成功";
        }
        request.getSession().setAttribute("msg", msg);
        response.sendRedirect("list");
    }
}
